﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class View_HomeContent : System.Web.UI.UserControl
{
    static CultureInfo cul;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            loadData();
        }
        
        
    }
    public void loadData()
    {        
        categoryBus catbus = new categoryBus();
        DataTable dt = catbus.getCategoriesByParentId(0,loadLanguage());
        listcategories.DataSource = dt;
        listcategories.DataBind();
    }
    public int loadLanguage()
    {
        try
        {
            HttpCookie cookie = Request.Cookies["language"];
            return Convert.ToInt32(cookie["language"].ToString());
        }
        catch (Exception)
        {
            return 1;
        }
    }
    protected void listcategories_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
    }
    public DataTable getArticeByID(int category_id, int language_id)
    {
        categoryBus catbus=new categoryBus();
        Article_BU articlebus=new Article_BU();
        DataTable dt = new DataTable();
        dt.Columns.Add("id");
        dt.Columns.Add("name");
        dt.Columns.Add("des");
        dt.Columns.Add("image");
        dt.Columns.Add("css");
        dt.Columns.Add("href");
        int i = 0;
        DataRow dr = null;
        DataTable listcat = catbus.getCategoriesByParentId(category_id, language_id);
        foreach (DataRow r in listcat.Rows)
        {
            DataTable listarticle = articlebus.getArticleByCategoryID(language_id, Convert.ToInt32(r["category_id"]), 1);
            foreach (DataRow a in listarticle.Rows)
            {
                dr = dt.NewRow();
                dr["id"] = a["article_id"];
                dr["name"] = a["article_name"];
                dr["des"] = a["article_sumary"];
                dr["image"] = "images/upload/"+a["image"];
                if (i != 0 && i % 3 == 0)
                {
                    dr["css"] = "one_third last";
                }
                else
                {
                    dr["css"] = "one_third";
                }
                dr["href"] = "default.aspx?action=article&id="+ a["article_id"];
                dt.Rows.Add(dr);
                i++;
            }
        }
        return dt;

    }
    protected void listcategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        
        Article_BU articlebus = new Article_BU();
        Label cat_id = e.Item.FindControl("category_id") as Label;
        int category_id = Convert.ToInt32(cat_id.Text);
        DataTable dt = this.getArticeByID(category_id, loadLanguage());
        Repeater listarticle = e.Item.FindControl("listarticles") as Repeater;
        
        if (listarticle != null)
        {
            if (dt.Rows.Count > 0)
            {
                listarticle.DataSource = dt;
                listarticle.DataBind();
            }
        }
    }
   
    
}